package com.leadnews;

import com.leadnews.mapper.NovelContentMapper;
import com.leadnews.mapper.NovelMapper;
import com.leadnews.pojo.Novel;
import com.leadnews.pojo.NovelContent;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@SpringBootTest
public class Demo {
    @Autowired
    NovelContentMapper novelContentMapper;

//    public static void main(String[] args) {
//        ArrayList<Integer> list = new ArrayList<>();
//        list.add(1);
//        list.add(2);
//        list.add(3);
//        /*使用函数式编程*/
//        /* 第一种方式*/
//        List<Integer> listAdd = list.stream().map(s -> s + 2).collect(Collectors.toList());
//        System.out.println("listAdd" + listAdd);
//
//    }

    public static void main(String[] args) {
        String s="1 1";
        String[] split = s.split(" ");
        System.out.println(split[0]);
    }
    @Test
    //爬虫小说 至尊剑皇
    public void JsoupDemo() throws IOException {
        ArrayList<String> list = new ArrayList<>();
        NovelContent novel = new NovelContent();
        Document document = Jsoup.connect("https://www.dingdianorg.com/lyd21743/")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36")
                .get();
            Elements contents = document.getElementsByTag("dd");
        for (int i = 0; i < contents.size(); i++) {
            if (i==14){
                Elements a = contents.get(i).getElementsByTag("a");
//                System.out.println(a);
                String s = a.get(0).toString();
                novel.setTitle(a.get(0).text());
                novel.setNovelId(1);
                System.out.println(a.get(0).text());
                String substring = s.substring(9, s.indexOf("."));
                String web="https://www.dingdianorg.com"+substring+".html";
                System.out.println(web);
                //抓取文章
                Document document1 = Jsoup.connect(web)
                        .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36")
                        .get();
                Elements showtxt = document1.getElementsByClass("showtxt");
                for (Element element : showtxt) {
                    System.out.println(element.text());
                    novel.setContent(element.text());
                }
                int insert = novelContentMapper.insert(novel);
                if (insert>0){
                    System.out.println("success");
                }else{
                    System.out.println("false");
                }


            }

        }



    }
    @Test
    //爬虫小说 斗破苍穹
    public void JsoupDemo2() throws IOException {
        ArrayList<String> list = new ArrayList<>();
        NovelContent novel = new NovelContent();
        Document document = Jsoup.connect("https://www.81zw.com/book/86166/")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36")
                .get();
        Elements contents = document.getElementsByTag("dd");
        for (int i = 0; i < contents.size(); i++) {
            if (i>=1641&&i<=1648){
                Elements a = contents.get(i).getElementsByTag("a");
//                System.out.println(a);
                String s = a.get(0).toString();
                novel.setTitle(a.get(0).text());
                novel.setNovelId(2);
                System.out.println(a.get(0).text());
                String substring = s.substring(9, s.indexOf("."));
                String web="https://www.81zw.com"+substring+".html";
                System.out.println(web);
                //抓取文章
                Document document1 = Jsoup.connect(web)
                        .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36")
                        .get();
                Element showtxt = document1.getElementById("content");

                    System.out.println(showtxt.text());
                    novel.setContent(showtxt.text());

                int insert = novelContentMapper.insert(novel);
                if (insert>0){
                    System.out.println("success");
                }else{
                    System.out.println("false");
                }


            }

        }



    }
    @Test
    //爬虫小说 我娘子一个比一个诡异
    public void JsoupDemo3() throws IOException {
        NovelContent novel = new NovelContent();
        Document document = Jsoup.parse(new URL("https://www.cv148.com/69_69870/").openStream(), "GBK", "https://www.cv148.com/69_69870/");//你可以试试GBK或UTF-8


        Elements contents = document.getElementsByTag("dd");
        for (int i = 0; i < contents.size(); i++) {
            if (i>=461){
                Elements a = contents.get(i).getElementsByTag("a");
//                System.out.println(a);
                String s = a.get(0).toString();
                novel.setTitle(a.get(0).text());
                novel.setNovelId(3);
                System.out.println(a.get(0).text());
                String substring = s.substring(9, s.indexOf("."));
                String web="https://www.cv148.com/69_69870/"+substring+".html";
                System.out.println(web);
                //抓取文章
//                Document document1 = Jsoup.connect(web)
//                        .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36")
//                        .get();
                Document document1 = Jsoup.parse(new URL(web).openStream(),"GBK",web);//你可以试试GBK或UTF-8

                Element showtxt = document1.getElementById("content");

                System.out.println(showtxt.text());
                novel.setContent(showtxt.text());
//                novelContentMapper.deleteByid("3");
                int insert = novelContentMapper.insert(novel);
                if (insert>0){
                    System.out.println("success");
                }else{
                    System.out.println("false");
                }


            }

        }



    }
}
